<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of order
 *
 * @author Administrator
 */
class order extends Controller {
    //put your code here
    function order() {
        parent::Controller();
        $this->load->model("post_model");
        $this->load->model("book_model");
        $this->load->model("user_model");
        $this->load->model("order_model");
        $this->load->helper('url');
        $this->load->library('mypagination');
    }

    //warning信息
    function warning( $warning ) {
        echo "<script charset=\"UTF-8\" language=\"javascript\">";
        echo "window.alert('" . $warning. "')";
        echo "</script>";
        echo "<script language=\"javascript\">";
        echo "location.href=\"javascript:history.go(-1)\"";
        echo "</script>";
        return;
    }

    //向书易购买 For customer to buy book from Bookiki
    function shuyi_wantedorder($warning="") {
        $this->load->library('session');
        $siteurl=site_url();
        $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        if(($this->session->userdata('username'))==""){
            $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
            Header("Location: $loginPage");
        }
        $data['warning'] = $warning;
        $data['baseurl']=base_url();
        $data['title']="向书易购买书籍";
        $data['isbn13']=$_GET['isbn13'];
        $data['book']=$this->book_model->get_shuyibook_detail($data['isbn13']); //在warehouse表中查询是否有书籍信息
        if($warning === "")
            $this->order_model->add2shop_car($this->session->userdata('username'),$_GET['isbn13'],$_GET['title']); //加入购物车
        $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('order/shuyi_wantedorder',$data);		//载入注册页面视图
    }

    //向书易出售 For customer to sell book to Bookiki
    function shuyi_saleorder() {
        $this->load->library('session');
        $siteurl=site_url();
        $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        if(($this->session->userdata('username'))==""){
            $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
            Header("Location: $loginPage");
        }
        $data['baseurl']=base_url();
        $data['title']="向书易出售书籍";
        $data['isbn13']=$_GET['isbn13'];
        $data['book']=$this->book_model->get_shuyibook_detail($data['isbn13']);
        $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('order/shuyi_saleorder',$data);		//载入注册页面视图
    }

    //填写订单详细信息
    function order_info($warning="") {
        $this->load->library('session');
        $siteurl=site_url();
        $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        if(($this->session->userdata('username'))==""){
            $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
            Header("Location: $loginPage");
        }
        $data['baseurl']=base_url();
        $data['title']="填写订单详细信息";
        $n=$_POST['num_kinds'];
        $order_info = null;
        for( $i=1,$j=0; $i <= $n; $i++ ){
            if( $_POST['order_number_'.$i] != 0 ){
                $order_info[$j]->book_id = $_POST['book_id_'.$i];
                $order_info[$j]->title = $_POST['title_'.$i];
                $order_info[$j]->price = $_POST['price_'.$i];
                $order_info[$j]->order_number = $_POST['order_number_'.$i];
                $order_info[$j]->total = intval($_POST['order_number_'.$i]) * $_POST['price_'.$i];
                $j++;
            }
        }
        if($order_info != null)
            $data['order_info']=$order_info;
        else{
            $_GET['isbn13'] = $_POST['isbn13'];
            $this->shuyi_wantedorder("订购书籍不能为零！");
            return;
            }
        $data['warning'] = $warning;
        $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        
        $this->load->view('order/order_detail',$data);
    }

    //查看所有订单
    function submit_order() {
        $this->load->library('session');
        $siteurl=site_url();
        $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        if(($this->session->userdata('username'))==""){
            $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
            Header("Location: $loginPage");
        }
        $data['baseurl']=base_url();
        $data['title']="提交订单";
        $userinfo=$this->user_model->get_userinfo($this->session->userdata('username'));
        $client = null;
        switch($userinfo->campus_id){
            case 1: $client->address="玉泉".$userinfo->dorm;break;
            case 2:$client->address="紫金港".$userinfo->dorm;break;
            case 3: $client->address="西溪".$userinfo->dorm;break;
            case 4: $client->address="华家池".$userinfo->dorm;break;
            case 5: $client->address="之江".$userinfo->dorm;break;
        }
        $client->consignee=$userinfo->name;
        $client->phone=$userinfo->phone;
        $client->short_phone=$userinfo->short_phone;
        $client->dorm=$userinfo->dorm;
        if( array_key_exists('consignee',$_POST)&&($client->consignee = $_POST['consignee']) === '' ){
            $this->order_info("收货人不能为空!");
            return;
        }
        if( array_key_exists("dorm",$_POST)&&($client->dorm=$_POST['dorm']) === ''&&($client->address.=$_POST['dorm'])){
            $this->order_info("收货地址不能为空!");
            return;
        }
        if( array_key_exists("phone",$_POST)&&($client->phone = $_POST['phone']) === '' ){
            $this->order_info("联系电话不能为空!");
            return;
        }
        if(array_key_exists('short_phone',$_POST))
            $client->short_phone=$_POST['short_phone'];
        $client->user_id = $this->session->userdata('username');
        
        $n=$_POST['num_kinds'];
        $order_info = null;
        for( $i=1,$j=0; $i <= $n; $i++ ){
            if( $_POST['order_number_'.$i] != 0 ){
                $order_info[$j]->book_id = $_POST['book_id_'.$i];
                //$order_info[$j]->title = $_GET['title_'.$i];
                //$order_info[$j]->price = $_GET['price_'.$i];
                $order_info[$j]->order_number = $_POST['order_number_'.$i];
                $order_info[$j]->total = $_POST['total_'.$i];
                $j++;
            }
        }

        $this->order_model->gen_order($client,$order_info); //生成订单
        $data['userinfo']=$this->user_model->get_userinfo($client->user_id);
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('order/order_suc',$data);		//载入注册页面视图
    }

    function my_order(){
        $this->load->library('session');
        $siteurl=site_url();
        $currentPage=$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        if(($this->session->userdata('username'))==""){
            $loginPage=$siteurl.'/user/login?continue=yes&currentpage='.urlencode($currentPage);
            Header("Location: $loginPage");
        }
        $data['baseurl']=base_url();
        $data['title']="我的订单";
        $user_id = $this->session->userdata('username');
        $data['my_order'] = $this->order_model->get_my_order($user_id); //得到所有订单详情
        $data['userinfo']=$this->user_model->get_userinfo($user_id);
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('order/my_order',$data);
    }
}
?>
